package com.example.textviewactivity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class CheckBoxWorkActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
    private static final String TAG = "CheckBoxWorkActivity";
    CheckBox huawei, xiaomi, meizu, vivo, oppo, total;
    CheckBox[] che;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_box_work);
        huawei = findViewById(R.id.huawei);
        huawei.setOnCheckedChangeListener(this);
        xiaomi = findViewById(R.id.xiaomi);
        xiaomi.setOnCheckedChangeListener(this);
        meizu = findViewById(R.id.meizu);
        meizu.setOnCheckedChangeListener(this);
        vivo = findViewById(R.id.vivo);
        vivo.setOnCheckedChangeListener(this);
        oppo = findViewById(R.id.oppo);
        oppo.setOnCheckedChangeListener(this);
        total = findViewById(R.id.total);
        total.setOnCheckedChangeListener(this);
        che=new CheckBox[]{huawei,xiaomi,meizu,vivo,oppo};
    }


    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        switch (compoundButton.getId()){
            case R.id.huawei:
            case R.id.xiaomi:
            case R.id.meizu:
            case R.id.vivo:
            case R.id.oppo:
                total.setOnCheckedChangeListener(null); //当点击其他按钮时,取消结算监听
                if (huawei.isChecked()&&xiaomi.isChecked()&&meizu.isChecked()&&vivo.isChecked()&&oppo.isChecked()){
                    //当除了结算按钮都选中,开启结算按钮监听
                    total.setOnCheckedChangeListener(this);
                    total.setChecked(true);
                }else{
                    total.setChecked(false);
                }
                break;
            case  R.id.total:
                if (b){
                    for (int i = 0; i < che.length; i++) {
                        che[i].setChecked(true);
                    }
                }else{
                    for (int i = 0; i < che.length; i++) {
                        che[i].setChecked(false);
                    }
                }

                break;
        }
    }
}
